package com.ys.malini.util;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import org.apache.struts.upload.FormFile;

public class FileUtils {
	
	public static void upload(FormFile file, String uploadPath) throws IOException {
		InputStream streamIn = null;
		OutputStream streamOut = null;
		String uploadPathAndFileName = uploadPath + file.getFileName();
		try {
			streamIn = file.getInputStream();// 把文件读入
			streamOut = new FileOutputStream(uploadPathAndFileName);// 建立一个上传文件的输出流,配置当前的上传路径
			int bytesRead = 0;
			byte[] buffer = new byte[8192];
			while ((bytesRead = streamIn.read(buffer, 0, 8192)) != -1) {
				streamOut.write(buffer, 0, bytesRead);// 把文件写入到服务器
			}
		} catch (FileNotFoundException e) {
			throw e;
		} catch (IOException ex) {
			throw ex;
		} finally {
			try {
				streamIn.close();
				streamOut.close();
				file.destroy();
			} catch (IOException e) {
				e.printStackTrace();
			}
			
		}
		
	}


}
